<template>
  <el-dialog v-model="dialogVisible" title="增加工作中心" width="900px">
    <el-form class="aceauForm" label-width="120px" :model="state.form">
      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item label="中心编码" required>
            <el-input v-model="state.form.wcCode" placeholder="请填写编码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="中心名称" required>
            <el-input v-model="state.form.wcName" placeholder="请填写编码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属部门">
            <el-select
              v-model="state.form.pkDept"
              placeholder="部门"
              style="width: 100%"
            >
              <el-option
                v-for="dept in depts"
                :key="dept.pkDept"
                :label="dept.deptName"
                :value="dept.pkDept"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备料仓库">
            <el-select
              v-model="state.form.blWhPk"
              placeholder="备料仓库"
              style="width: 100%"
            >
              <el-option
                v-for="wh in whs"
                :key="wh.pkWh"
                :label="wh.whName"
                :value="wh.pkWh"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="默认仓库">
            <el-select
              v-model="state.form.defaultRkWhPk"
              placeholder="默认入库仓库"
              style="width: 100%"
            >
              <el-option
                v-for="wh in whs"
                :key="wh.pkWh"
                :label="wh.whName"
                :value="wh.pkWh"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="使用模板">
            <el-checkbox v-model="state.form.useMold" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="序列号管理">
            <el-checkbox v-model="state.form.useSn" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
  import { getWarehouseListApi } from '@/api/base/wh'
  import { getDeptListApi } from '@/api/base/dept'
  import { addWcApi } from '@//api/base/wc'
  import { localStorageConfig } from '@/config/localstorage.config'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)
  const dialogVisible = ref(false)

  const state = reactive({
    form: {},
  })

  const whs = ref()
  const depts = ref()

  const openDialog = () => {
    getWarehouseList()
    getDepts()
    dialogVisible.value = true
  }

  const getWarehouseList = async () => {
    let con = { pkCorp: pkCorp }
    const res = await getWarehouseListApi(con)
    if (res.code === 2000) {
      whs.value = res.data
    }
  }

  const getDepts = async () => {
    let con = { pkCorp: pkCorp, isProduce: true }
    const res = await getDeptListApi(con)
    if (res.code === 2000) {
      depts.value = res.data
    }
  }

  const save = async () => {
    const load = $baseLoading()
    try {
      state.form.pkCorp = pkCorp
      const res = await addWcApi(state.form)
      if (res.code === 2000) {
        $baseMessage(res.msg, 'success')
        dialogVisible.value = false
        _emit('success-callback')
      } else {
        $baseMessage(res.msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  defineExpose({ openDialog })
  const _emit = defineEmits(['success-callback'])
</script>
